﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using University.Workshop.Model;
using University.Workshop.Business;
using System.IO;

namespace University.Workshop.WinApp
{
    public partial class TreatmentPatient : Form
    {
        Patient patient = new Patient();
        OpenFileDialog openFile = new OpenFileDialog();
        public TreatmentPatient(Patient queryPatient)
        {
            InitializeComponent();
            patient = queryPatient;
            LoadQueryPatient();
            ObtainNQuery();
            ObtainAlertsPatient();
            //IssuePermits();
        }

        //private void IssuePermits()
        //{
        //    //buttonSave.Enabled = RoleFacade.PermissionUser(Main.userMenu[3], "");
        //}

        private void ObtainAlertsPatient()
        {
            textBoxAlertsPatient.Text = DiagnosticManager.LoadDataDiagnosticPatient(patient.CIPatient);
        }
        private void LoadQueryPatient()
        {
            Random randon = new Random(DateTime.Now.Millisecond);
            int age = patient.Birthday.Year;
            textBoxCodeTreatment.Text = Convert.ToString(randon.Next(10000));
            textBoxFullName.Text= patient.LastName;
            textBoxFullName.Text += ", ";
            textBoxFullName.Text += patient.FirstName;
            textBoxCiPatient.Text = patient.CIPatient;
            textBoxAge.Text = Convert.ToString(DateTime.Today.Year - age);           
            textBoxGender.Text = patient.Gender;            
        }
        private void ObtainNQuery()
        {            
            textBoxCodeDiagnostic.Text = DiagnosticManager.ObtainNunQuery();
        }

        private void buttonD18_Click(object sender, EventArgs e)
        {
            textBoxNroTooth.Text = "18";
            textBoxType.Text = "Molar";
            textBoxLocation.Text = "Top Left Maxillary";
            comboBoxFaceTooth.Text = "Distal";
        }

        private void buttonD17_Click(object sender, EventArgs e)
        {
            textBoxNroTooth.Text = "17";
            textBoxType.Text = "Molar";
            textBoxLocation.Text = "Top Left Maxillary";
            comboBoxFaceTooth.Text = "Distal";
        }

        private void buttonD16_Click(object sender, EventArgs e)
        {
            textBoxNroTooth.Text = "16";
            textBoxType.Text = "Molar";
            textBoxLocation.Text = "Top Left Maxillary";
            comboBoxFaceTooth.Text = "Distal";
        }

        private void buttonD15_Click(object sender, EventArgs e)
        {
            textBoxNroTooth.Text = "15";
            textBoxType.Text = "Premolar";
            textBoxLocation.Text = "Top Left Maxillary";
            comboBoxFaceTooth.Text = "Distal";
        }

        private void buttonD14_Click(object sender, EventArgs e)
        {
            textBoxNroTooth.Text = "14";
            textBoxType.Text = "Premolar";
            textBoxLocation.Text = "Top Left Maxillary";
            comboBoxFaceTooth.Text = "Distal";
        }

        private void buttonD13_Click(object sender, EventArgs e)
        {
            textBoxNroTooth.Text = "13";
            textBoxType.Text = "Canino";
            textBoxLocation.Text = "Top Left Maxillary";
            comboBoxFaceTooth.Text = "Distal";
        }

        private void buttonD12_Click(object sender, EventArgs e)
        {
            textBoxNroTooth.Text = "12";
            textBoxType.Text = "Incisivo";
            textBoxLocation.Text = "Top Left Maxillary";
            comboBoxFaceTooth.Text = "Distal";
        }

        private void buttonD11_Click(object sender, EventArgs e)
        {
            textBoxNroTooth.Text = "11";
            textBoxType.Text = "Incisivo";
            textBoxLocation.Text = "Top Left Maxillary";
            comboBoxFaceTooth.Text = "Distal";
        }

        private void buttonD21_Click(object sender, EventArgs e)
        {
            textBoxNroTooth.Text = "21";
            textBoxType.Text = "Incisivo";
            textBoxLocation.Text = "Top Right Maxillary";
            comboBoxFaceTooth.Text = "Distal";
        }

        private void buttonD22_Click(object sender, EventArgs e)
        {
            textBoxNroTooth.Text = "22";
            textBoxType.Text = "Incisivo";
            textBoxLocation.Text = "Top Right Maxillary";
            comboBoxFaceTooth.Text = "Distal";
        }

        private void buttonD23_Click(object sender, EventArgs e)
        {
            textBoxNroTooth.Text = "23";
            textBoxType.Text = "Canino";
            textBoxLocation.Text = "Top Right Maxillary";
            comboBoxFaceTooth.Text = "Distal";
        }

        private void buttonD24_Click(object sender, EventArgs e)
        {
            textBoxNroTooth.Text = "24";
            textBoxType.Text = "Premolar";
            textBoxLocation.Text = "Top Right Maxillary";
            comboBoxFaceTooth.Text = "Distal";
        }

        private void buttonD25_Click(object sender, EventArgs e)
        {
            textBoxNroTooth.Text = "25";
            textBoxType.Text = "Premolar";
            textBoxLocation.Text = "Top Right Maxillary";
            comboBoxFaceTooth.Text = "Distal";
        }

        private void buttonD26_Click(object sender, EventArgs e)
        {
            textBoxNroTooth.Text = "26";
            textBoxType.Text = "Molar";
            textBoxLocation.Text = "Top Right Maxillary";
            comboBoxFaceTooth.Text = "Distal";
        }

        private void buttonD27_Click(object sender, EventArgs e)
        {
            textBoxNroTooth.Text = "27";
            textBoxType.Text = "Molar";
            textBoxLocation.Text = "Top Right Maxillary";
            comboBoxFaceTooth.Text = "Distal";
        }

        private void buttonD28_Click(object sender, EventArgs e)
        {
            textBoxNroTooth.Text = "28";
            textBoxType.Text = "Molar";
            textBoxLocation.Text = "Top Right Maxillary";
            comboBoxFaceTooth.Text = "Distal";
        }

        private void buttonD48_Click(object sender, EventArgs e)
        {
            textBoxNroTooth.Text = "48";
            textBoxType.Text = "Molar";
            textBoxLocation.Text = "Lower Right Maxillary";
            comboBoxFaceTooth.Text = "Distal";
        }

        private void buttonD47_Click(object sender, EventArgs e)
        {
            textBoxNroTooth.Text = "47";
            textBoxType.Text = "Molar";
            textBoxLocation.Text = "Lower Right Maxillary";
            comboBoxFaceTooth.Text = "Distal";
        }

        private void buttonD46_Click(object sender, EventArgs e)
        {
            textBoxNroTooth.Text = "46";
            textBoxType.Text = "Molar";
            textBoxLocation.Text = "Lower Right Maxillary";
            comboBoxFaceTooth.Text = "Distal";
        }

        private void buttonD45_Click(object sender, EventArgs e)
        {
            textBoxNroTooth.Text = "45";
            textBoxType.Text = "Premolar";
            textBoxLocation.Text = "Lower Right Maxillary";
            comboBoxFaceTooth.Text = "Distal";
        }

        private void buttonD44_Click(object sender, EventArgs e)
        {
            textBoxNroTooth.Text = "44";
            textBoxType.Text = "Premolar";
            textBoxLocation.Text = "Lower Right Maxillary";
            comboBoxFaceTooth.Text = "Distal";
        }

        private void buttonD43_Click(object sender, EventArgs e)
        {
            textBoxNroTooth.Text = "43";
            textBoxType.Text = "Canino";
            textBoxLocation.Text = "Lower Right Maxillary";
            comboBoxFaceTooth.Text = "Distal";
        }

        private void buttonD42_Click(object sender, EventArgs e)
        {
            textBoxNroTooth.Text = "42";
            textBoxType.Text = "Incisivo";
            textBoxLocation.Text = "Lower Right Maxillary";
            comboBoxFaceTooth.Text = "Distal";
        }

        private void buttonD41_Click(object sender, EventArgs e)
        {
            textBoxNroTooth.Text = "41";
            textBoxType.Text = "Incisivo";
            textBoxLocation.Text = "Lower Right Maxillary";
            comboBoxFaceTooth.Text = "Distal";
        }

        private void buttonD31_Click(object sender, EventArgs e)
        {
            textBoxNroTooth.Text = "31";
            textBoxType.Text = "Incisivo";
            textBoxLocation.Text = "Lower Left Maxillary";
            comboBoxFaceTooth.Text = "Distal";
        }

        private void buttonD32_Click(object sender, EventArgs e)
        {
            textBoxNroTooth.Text = "32";
            textBoxType.Text = "Incisivo";
            textBoxLocation.Text = "Lower Left Maxillary";
            comboBoxFaceTooth.Text = "Distal";
        }

        private void buttonD33_Click(object sender, EventArgs e)
        {
            textBoxNroTooth.Text = "33";
            textBoxType.Text = "Canino";
            textBoxLocation.Text = "Lower Left Maxillary";
            comboBoxFaceTooth.Text = "Distal";
        }

        private void buttonD34_Click(object sender, EventArgs e)
        {
            textBoxNroTooth.Text = "34";
            textBoxType.Text = "Premolar";
            textBoxLocation.Text = "Lower Left Maxillary";
            comboBoxFaceTooth.Text = "Distal";
        }

        private void buttonD35_Click(object sender, EventArgs e)
        {
            textBoxNroTooth.Text = "35";
            textBoxType.Text = "Premolar";
            textBoxLocation.Text = "Lower Left Maxillary";
            comboBoxFaceTooth.Text = "Distal";
        }

        private void buttonD36_Click(object sender, EventArgs e)
        {
            textBoxNroTooth.Text = "36";
            textBoxType.Text = "Molar";
            textBoxLocation.Text = "Lower Left Maxillary";
            comboBoxFaceTooth.Text = "Distal";
        }

        private void buttonD37_Click(object sender, EventArgs e)
        {
            textBoxNroTooth.Text = "37";
            textBoxType.Text = "Molar";
            textBoxLocation.Text = "Lower Left Maxillary";
            comboBoxFaceTooth.Text = "Distal";
        }

        private void buttonD38_Click(object sender, EventArgs e)
        {
            textBoxNroTooth.Text = "38";
            textBoxType.Text = "Molar";
            textBoxLocation.Text = "Lower Left Maxillary";
            comboBoxFaceTooth.Text = "Distal";
        }

        //private void buttonLoadImage_Click(object sender, EventArgs e)
        //{
        //    if (openFile.ShowDialog() == System.Windows.Forms.DialogResult.OK) 
        //    {
        //        pictureBoxImageXRay.ImageLocation = openFile.FileName;
        //    }
        //}

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //private void buttonViewImage_Click(object sender, EventArgs e)
        //{
        //    ViewImageXRay viewImage = new ViewImageXRay(pictureBoxImageXRay.Image);
        //    viewImage.ShowDialog();
        //}

        //private void buttonRevertImage_Click(object sender, EventArgs e)
        //{
        //    pictureBoxImageXRay.Image = global::University.Workshop.WinApp.Properties.Resources.tooth;
        //}

        private void buttonSave_Click(object sender, EventArgs e)
        {
            Treatment treatment = new Treatment();
            GeneralTreatment generalTreatment = new GeneralTreatment();
            ToothTreatment toothTreatment = new ToothTreatment();
            treatment.CodeTreatment = int.Parse(textBoxCodeTreatment.Text);
            treatment.CodeDiagnostic = int.Parse(textBoxCodeDiagnostic.Text);
            treatment.State = "Activo";

            generalTreatment.CodeTreatment = int.Parse(textBoxCodeTreatment.Text);
            if (!string.IsNullOrEmpty(comboBoxOrthodontics.Text)&& radioButton1.Checked==true) 
            {
                generalTreatment.Treatment= comboBoxOrthodontics.Text; 
            }
            if (!string.IsNullOrEmpty(comboBoxPeriodontics.Text) && radioButton2.Checked==true) 
            {
                generalTreatment.Treatment = comboBoxPeriodontics.Text; 
            }
            if (!string.IsNullOrEmpty(comboBoxHygiene.Text)&& radioButton3.Checked== true)
            {
                generalTreatment.Treatment = comboBoxHygiene.Text;
            }            
            generalTreatment.Description = textBoxGeneralDescription.Text;
            
            toothTreatment.CodeTreatment = int.Parse(textBoxCodeTreatment.Text);                       
            if (textBoxNroTooth.Text != "")
            {
                toothTreatment.ToothCode = int.Parse(textBoxNroTooth.Text);
            }
            else
            {
                toothTreatment.ToothCode = 0;
            }

            toothTreatment.ToothCode = int.Parse(textBoxNroTooth.Text);
            toothTreatment.ToothType = textBoxType.Text;
            toothTreatment.ToothLocation = textBoxLocation.Text;
            toothTreatment.Treatment = comboBoxTreatmentTooth.Text;
            toothTreatment.TreatmentDescription = textBoxTreatmentTooth.Text;
            toothTreatment.ToothImage = openFile.SafeFileName;
            if (TreatmentManager.NewTreatmentPatient(treatment, generalTreatment, toothTreatment))
            {
                MessageBox.Show("Successful registration");                
                try
                {
                    File.Copy(openFile.FileName, Path.Combine(Application.StartupPath, string.Format("Pictures\\{0}", openFile.SafeFileName)));

                }
                catch (Exception)
                {

                }
                this.Close();
            }
            else 
            {
                MessageBox.Show("The Treatment Patient could not be registered");
            }
        }

        private void comboBoxFaceTooth_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBoxFaceTooth.Text == "Distal") 
            {
                pictureBoxTooth.Image = global::University.Workshop.WinApp.Properties.Resources.ToothDistal;
            }
            if (comboBoxFaceTooth.Text == "Mesial")
            {
                pictureBoxTooth.Image = global::University.Workshop.WinApp.Properties.Resources.ToothMesial;
            }
            if (comboBoxFaceTooth.Text == "Oral")
            {
                pictureBoxTooth.Image = global::University.Workshop.WinApp.Properties.Resources.ToothOral;
            }
            if (comboBoxFaceTooth.Text == "Vestivular")
            {
                pictureBoxTooth.Image = global::University.Workshop.WinApp.Properties.Resources.ToothVestibular;
            }
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Radiographie radio = new Radiographie();
            radio.ShowDialog();
        }

        //private void comboBoxOrthodontics_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    comboBoxPeriodontics.Text = null;
        //    comboBoxHygiene.Text = null;            
        //}

        //private void comboBoxPeriodontics_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    comboBoxHygiene.Text = null;
        //    comboBoxOrthodontics.Text = null;
        //}

        //private void comboBoxHygiene_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    comboBoxOrthodontics.Text = null;
        //    comboBoxPeriodontics.Text = null;
        //}

        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            //radioButton1.Checked = false;
            //radioButton2.Checked = false;
        }
    }
}
